package com.ishansong.ticket.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ishansong.ticket.exception.ServiceDaoException;
import com.ishansong.ticket.exception.ServiceException;
import com.ishansong.ticket.model.TicketUser;
import com.ishansong.ticket.service.TicketUserService;
import com.matrixjoy.dal.dao.Dao;
import com.matrixjoy.dal.dao.exception.DaoException;



@Component( value= "ticketUserService" ) 
public class TicketUserServiceImpl implements TicketUserService {

	
	@Autowired
	private Dao dao;

	@Override
	public Boolean deleteTicketUser(Long id) throws ServiceDaoException, ServiceException {
		try {
			return (Boolean) dao.delete(TicketUser.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public TicketUser queryTicketUserByMobile(String mobile) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("queryTicketUserByMobile", new Object[] { mobile });
			if (!CollectionUtils.isEmpty(ids)) {
				List<TicketUser> ticketQaGroups = dao.getList(TicketUser.class, ids);
				return ticketQaGroups.get(0);
			} else {
				return null;
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public TicketUser getTicketUser(Long id) throws ServiceDaoException, ServiceException {
		try {
			if (null == id) {
				return null;
			}
			return (TicketUser) dao.get(TicketUser.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public Long saveTicketUser(TicketUser ticketUser) throws ServiceDaoException, ServiceException {
		try {
			return (Long) dao.save(ticketUser);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	@Override
	public void updateTicketUser(TicketUser ticketUser) throws ServiceDaoException, ServiceException {
		try {
			dao.update(ticketUser);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}

	}

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
